/*--------------------------------------------------------------------------------
 * Adaptive Intelligence - Foundation Framework
 * Version 2.10
 * 
 * Stored Procedure Result Class
 *      Represents and manages the result of the attempt to execute a stored procedure.
 *------------------------------------------------------------------------------*/

/*--------------------------------------------------------------------------------
 * System Imports
 *------------------------------------------------------------------------------*/
using System;
using System.Data;

namespace Adaptive.Foundation.Data
{
    /// <summary>
    /// Represents and manages the result of the attempt to execute a stored procedure.
    /// </summary>
    /// <remarks>
    /// This is used in conjunction with the <see cref="StoredProcedureBase"/> class and the 
    /// <see cref="DataActorBase"/> class in order to provide a managed container for the 
    /// results of stored procedure execution.  The contents of the object may contain the 
    /// rows affected, the single or multiple datasets, or the exception that may occur
    /// during execution.
    /// </remarks>
    public class StoredProcedureResult : DisposableObjectBase
    {
        /*--------------------------------------------------------------------------------
		 * Private Member Declarations
		 *------------------------------------------------------------------------------*/
        #region Private Member Declarations
        /// <summary>
        /// The exception that was caught during execution.
        /// </summary>
        private Exception _lastException;
        /// <summary>
        /// Contains the resulting dataset, if provided.
        /// </summary>
        private DataSet _datasetResult;
        /// <summary>
        /// Contains the resulting data table, if provided.
        /// </summary>
        private DataTable _dataTableResult;
        /// <summary>
        /// Success or failure flag.
        /// </summary>
        private bool _success;
        /// <summary>
        /// The number of rows affected by the query.
        /// </summary>
        private int _rowsAffected;
        /// <summary>
        /// Type of result expected.
        /// </summary>
        private StoredProcedureResultType _resultType = StoredProcedureResultType.None;
        #endregion

        /*--------------------------------------------------------------------------------
         * Constructor / Dispose / Destructor Methods
		 *------------------------------------------------------------------------------*/
        #region Constructor / Dispose / Destructor Methods
        /// <summary>
        /// Initializes a new instance of the class.
        /// </summary>
        /// <remarks>
        /// This is the default constructor.
        /// </remarks>
        public StoredProcedureResult()
            : base()
        {
        }
        /// <summary>
        /// Initializes a new instance of the class.
        /// </summary>
        /// <param name="resultType">
        /// A <see cref="StoredProcedureResultType"/> enumerated value indicating the
        /// type of result the stored procedure is expected to return.
        /// </param>
        public StoredProcedureResult(StoredProcedureResultType resultType)
            : base()
        {
            _resultType = resultType;
        }
        /// <summary>
        /// Initializes a new instance of the class.
        /// </summary>
        /// <param name="rowsAffected">
        /// The number of rows affected by the query after a successful result.
        /// </param>
        public StoredProcedureResult(int rowsAffected)
        {
            _resultType = StoredProcedureResultType.RowCount;
            _success = true;
            _rowsAffected = rowsAffected;
        }
        /// <summary>
        /// Initializes a new instance of the class.
        /// </summary>
        /// <param name="results">
        /// A <see cref="DataTable"/> containing the resultset returned by the query.
        /// </param>
        public StoredProcedureResult(DataTable results)
        {
            _resultType = StoredProcedureResultType.DataTable;
            _success = true;
            _dataTableResult = results;
        }
        /// <summary>
        /// Initializes a new instance of the class.
        /// </summary>
        /// <param name="results">
        /// A <see cref="DataSet"/> containing the multiple resultsets returned by the query.
        /// </param>
        public StoredProcedureResult(DataSet results)
        {
            _resultType = StoredProcedureResultType.DataSet;
            _success = true;
            _datasetResult = results;
        }
        /// <summary>
        /// Initializes a new instance of the class.
        /// </summary>
        /// <param name="storedProcException">
        /// The <see cref="Exception"/> that caused the query to fail.
        /// </param>
        public StoredProcedureResult(Exception storedProcException)
        {
            _lastException = storedProcException;
        }
        /// <summary>
        /// Initializes a new instance of the class.
        /// </summary>
        /// <param name="success">
        /// A value indicating whether the query was successful.
        /// </param>
        public StoredProcedureResult(bool success)
        {
            _success = success;
        }
        /// <summary>
        /// Deallocates internally allocated objectts.
        /// </summary>
        /// <param name="disposing">
        /// A value indicating whether the object is being disposed.
        /// </param>
        protected override void Dispose(bool disposing)
        {
            _lastException = null;
            _datasetResult = null;
            _dataTableResult = null;

            base.Dispose(disposing);
        }
        #endregion

        /*--------------------------------------------------------------------------------
	     * Public Properties
		 *------------------------------------------------------------------------------*/
        #region Public Properties
        /// <summary>
        /// Gets or s4ets the reference to the exception that was caught during execution.
        /// </summary>
        /// <value>
        /// An <see cref="Exception"/> instance or <b>null</b>.
        /// </value>
        public Exception LastException
        {
            get { return _lastException; }
            set { _lastException = value; }
        }
        /// <summary>
        /// Gets or sets the reference to the resulting data set.
        /// </summary>
        /// <value>
        /// A <see cref="DataSet"/> instance if the stored procedure returns multiple resultsets;
        /// otherwise, <b>null</b>.
        /// </value>
        public DataSet DatasetResult
        {
            get { return _datasetResult; }
            set { _datasetResult = value; }
        }
        /// <summary>
        /// Gets or sets the reference to the resulting data table.
        /// </summary>
        /// <value>
        /// A <see cref="DataTable"/> instance if the stored procedure returns a single resultset;
        /// otherwise, <b>null</b>.
        /// </value>
        public DataTable DataTableResult
        {
            get { return _dataTableResult; }
            set { _dataTableResult = value; }
        }
        /// <summary>
        /// Gets or sets a value indicating whether the related stored procedure was successful.
        /// </summary>
        /// <value>
        /// <b>true</b> if the stored procedure executed successfully; otherwise, <b>false</b>.
        /// </value>
        public bool Success
        {
            get { return _success; }
            set { _success = value; }
        }
        /// <summary>
        /// Gets or sets the number of rows affected by the query.
        /// </summary>
        /// <value>
        /// The rows affected value. if supplied; otherwise, zero (0).
        /// </value>
        public int RowsAffected
        {
            get { return _rowsAffected; }
            set { _rowsAffected = value; }
        }
        /// <summary>
        /// Gets or sets the type of result expected.
        /// </summary>
        /// <value>
        /// A <see cref="StoredProcedureResultType"/> enumerated type that indicates 
        /// the result expected by the related stored procedure.
        /// </value>
        public StoredProcedureResultType ResultType
        {
            get { return _resultType; }
            set { _resultType = value; }
        }
        #endregion
    }
}